/**
 * DataBaseFieldBuilder.java	1.0 2007 六月 6 11:45:14,创建
 */


package org.in4j.search.index.sandbox;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.document.Field;

/**
 *
 * @author 杨帆
 * @version 1.0 2007 六月 6 11:45:14,创建
 */
public class DataBaseFieldBuilder implements FieldBuilder
{
	@SuppressWarnings("unused")
	private static final Log log = LogFactory.getLog(DataBaseFieldBuilder.class);
	private final ResultSet rs;
	private final FieldMetaData meta;

	public DataBaseFieldBuilder(ResultSet rs, FieldMetaData meta)
	{
		this.rs = rs;
		this.meta = meta;
	}

	public Field buildField()
	{
		try
		{
			FieldConverter converter = meta.getFieldConverter();
			final String value;
			if(meta.isDate())
			{
				Date date;
				date = rs.getDate(meta.getSourceName());
				value = converter.convert(date);
			}
			else
			{
				String str = rs.getString(meta.getSourceName());
				value = converter.convert(str);
			}
			Field field = meta.createField(value);
			return field;
		}
		catch(SQLException ex)
		{
			throw new RuntimeException("构建索引失败,数据库发生错误.", ex);
		}
	}
}
